package com.swak.frame.i18n;

import com.swak.frame.util.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@Slf4j
public class MessageI18nSourceUtil {
    private static MessageI18nSource messageSource;

    public static void setMessageSource(MessageI18nSource source) {
        messageSource = source;
    }

    public static String getMessage(String code, Object... args) {
        if (Objects.isNull(messageSource)) {
            log.debug("MessageSource not config,please config i18n messageSource");
            return StringPool.EMPTY;
        }
        if(StringUtils.isEmpty(code)) {
            return StringPool.EMPTY;
        }
        return messageSource.getMessage(code, args);
    }

    public static String getMessage(String code, String defaultMessage) {
        if (Objects.isNull(messageSource)) {
            log.debug("MessageSource not config,please config i18n messageSource");
            return defaultMessage;
        }

        if(StringUtils.isEmpty(code)){
            return defaultMessage;
        }
        return messageSource.getMessage(code, defaultMessage);
    }

    public static String getMessage(String code, String defaultMessage, Object... args) {
        if (Objects.isNull(messageSource)) {
            log.debug("MessageSource not config,please config i18n messageSource");
            return defaultMessage;
        }

        if(StringUtils.isEmpty(code)){
            return defaultMessage;
        }

        return messageSource.getMessage(code, defaultMessage, args);
    }
}
